package com.smart.stock.common.util;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;


public class DateUtils {
    /** 默认时间格式 */
    public static String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    //下划线时间格式,不会生成在javadoc中
    public static String yyyyMMddHHmmss_ = "yyyy_MM_dd_HH_mm_ss";
    public static String yyyyMMdd_ = "yyyy-MM-dd";
    public static String yyyyMMdd = "yyyyMMdd";

    public static DateTimeFormatter defaultDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static DateTimeFormatter defaultDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static DateTimeFormatter defaultTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

//    LocalDateTime localDateTime = LocalDateTime.parse("2019-07-31 00:00:00",dateTimeFormatter1);
//    LocalDate localDate = LocalDate.parse("2019-07-31",dateTimeFormatter2);
//    Date date = Date.from(LocalDateTime.parse("2019-07-31 00:00:00",dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant());


    public static String current_date(){
        LocalDate now = LocalDate.now();
        String format = defaultDateFormatter.format(now);
        return format;
    }

    public static String current_date_yyyymmdd(){
        return current_date().replace("-","");
    }

    public static String current_time(){
        LocalDateTime now = LocalDateTime.now();
        String format = defaultTimeFormatter.format(now);
        return format;
    }

    public static String current_datetime(){
        LocalDateTime now = LocalDateTime.now();
        String format = defaultDateTimeFormatter.format(now);
        return format;
    }


    public static String long2string(Long timestamp){
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDate= LocalDateTime.ofInstant(instant, zone);
        return defaultDateFormatter.format(localDate);
    }

    public static LocalDate string2date(String str){
        LocalDate localDate = LocalDate.parse(str, defaultDateFormatter);
        return localDate;
    }

    /**将时间转换成字符，如DateUtil.to_char(new Date(),DateUtil.defaultFormat)
     * 	 * @param date 传入时间
     * @param pattern 传入格式
     * @return 返回字符
     */
    public static String toChar(LocalDateTime localDateTime, String pattern){
        String format = localDateTime.format(DateTimeFormatter.ofPattern(pattern));
        return format;
    }

    public static long toLong(LocalDateTime localDateTime){
        long l = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        return l;
    }


    public static String toChar(LocalDateTime localDateTime){
        return toChar(localDateTime, yyyyMMddHHmmss);
    }

    public static LocalDateTime toDate(String localDateTime){
        return toDate(localDateTime, yyyyMMddHHmmss);
    }

    public static LocalDateTime toDate(String localDateTime,String formatString){
        Date parse = null;
        if (formatString.equals("")||formatString==null) {
            formatString= yyyyMMddHHmmss;
        }
        LocalDateTime res = LocalDateTime.parse(localDateTime, DateTimeFormatter.ofPattern(formatString));
        return res;
    }

    public static LocalDateTime trunc(LocalDateTime localDateTime) throws Exception{
        return toDate(toChar(localDateTime), yyyyMMdd_);
    }

    public static LocalDateTime trunc() throws Exception{
        return trunc(LocalDateTime.now());
    }

    public static LocalDateTime trunc(LocalDateTime localDateTime,String formatString) {
        return toDate(toChar(localDateTime), formatString);
    }

    public static String trunc2char(LocalDateTime localDateTime,String formatString) {
        return toChar(trunc(localDateTime,formatString));
    }

    public static String minusDate(int year, int month, int day,String date) {
        LocalDate localDate = toDate(date, yyyyMMdd_).toLocalDate();
        return DateTimeFormatter.ofPattern(yyyyMMdd_).format(localDate.minusYears(year).minusMonths(month).minusDays(day));
    }

    public static LocalDateTime nYearsAgo(LocalDateTime localDateTime,int n){
        return localDateTime.minusYears(n);
    }

    public static LocalDateTime nMonthsAgo(LocalDateTime localDateTime,int n){
        return localDateTime.minusMonths(n);
    }

    public static LocalDateTime nDaysAgo(LocalDateTime localDateTime,int n){
        return localDateTime.minusDays(n);
    }

    public static LocalDateTime getFirstNDayOfMonth(LocalDateTime localDateTime,int n){
        //todo
        return null;
    }

    public static LocalDateTime getFirstDayOfYear(LocalDateTime localDateTime){
        //todo
        return null;
    }

    public static LocalDateTime getFirstDayOfMonth(LocalDateTime localDateTime){
        //todo
        return null;
    }

    public static LocalDateTime getLastNDayOfMonth(LocalDateTime localDateTime,int n){
        //todo
        return null;
    }

    public static int getWeek(LocalDateTime localDateTime){
        //todo
        return 0;
    }


    public static ArrayList<Object> getWeeks(LocalDateTime localDateTime, int lastNWeeks){
        //todo
        return null;
    }

    public static LocalDateTime getLastDayOfMonth(LocalDateTime localDateTime){
        //todo
        return null;
    }

    public static long getDays(LocalDateTime beginDate, LocalDateTime endDate){
        Instant instant1 = beginDate.atZone(ZoneId.systemDefault()).toInstant();
        Instant instant2 = endDate.atZone(ZoneId.systemDefault()).toInstant();
        long until = instant1.until(instant2, ChronoUnit.DAYS);
        return until;
    }


    public static void test(String[] args) {
//        Integer datatime = 300;
        LocalDateTime now = LocalDateTime.now();

        System.out.println(now);//2021-06-22T16:46:21.163
        //LocalDateTime 转 Long
        System.out.println(now.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());//1624351735968

        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = now.atZone(zoneId);
        Instant instant = zonedDateTime.toInstant();
        long l = instant.toEpochMilli();
        System.out.println(l);//1624351735968


//        now = now.truncatedTo(ChronoUnit.DAYS);
//        now = now.plusSeconds(datatime);
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        String res = now.format(formatter);
//        System.out.println(res);
//
//
//        LocalTime time1 = LocalTime.parse("08:03:46");
//        LocalTime time2 = LocalTime.parse("08:03:46");
//        System.out.println(time1.isAfter(time2));

//        LocalTime datePointPased = LocalTime.parse("06:09:00");
//        LocalTime minTimePased = LocalTime.parse("11:30:00");
//        LocalTime maxTimePased = LocalTime.parse("11:45:00");
//        if ((datePointPased.isBefore(minTimePased)||datePointPased.isAfter(maxTimePased))) {
//            System.out.println(1);
//        } else {
//            System.out.println(2);
//        }



        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDateTime localDateTime = LocalDateTime.parse("2019-07-31 00:00:00",dateTimeFormatter1);
        LocalDate localDate = LocalDate.parse("2019-07-31",dateTimeFormatter2);
        Date date = Date.from(LocalDateTime.parse("2019-07-31 00:00:00",dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant());


        String strDateTime = "2019-07-31 00:00:00";
        String strDate = "2019-07-31";
        Long timestamp=1564502400000l;

/** LocalDateTime 转 LocalDate */
        System.out.println("LocalDateTime 转 LocalDate: "+localDateTime.toLocalDate());
/** LocalDateTime 转 Long */
        System.out.println("LocalDateTime 转 Long: "+localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
/** LocalDateTime 转 Date */
        System.out.println("LocalDateTime 转 Date: "+Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));
/** LocalDateTime 转 String */
        System.out.println("LocalDateTime 转 String: "+localDateTime.format(dateTimeFormatter1));

        System.out.println("-------------------------------");

/** LocalDate 转 LocalDateTime */
        System.out.println("LocalDate 转 LocalDateTime: "+LocalDateTime.of(localDate,LocalTime.parse("00:00:00")));
/** LocalDate 转 Long */
        System.out.println("LocalDate 转 Long: "+localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli());
/** LocalDate 转 Date */
        System.out.println("LocalDate 转 Date: "+ Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
/** LocalDate 转 String */
        System.out.println("LocalDateTime 转 String: "+localDateTime.format(dateTimeFormatter2));

        System.out.println("-------------------------------");

/** Date 转 LocalDateTime */
        System.out.println("Date 转 LocalDateTime: "+LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
/** Date 转 Long */
        System.out.println("Date 转 Long: "+date.getTime());
/** Date 转 LocalDate */
        System.out.println("Date 转 LocalDateTime: "+LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate());
/** Date 转 String */
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS" );
        System.out.println("Date 转 String: "+sdf.format(date));

        System.out.println("-------------------------------");

/** String 转 LocalDateTime */
        System.out.println("String 转 LocalDateTime: "+LocalDateTime.parse(strDateTime,dateTimeFormatter1));
/** String 转 LocalDate */
        System.out.println("String 转 LocalDate: "+LocalDateTime.parse(strDateTime,dateTimeFormatter1).toLocalDate());
        System.out.println("String 转 LocalDate: "+LocalDate.parse(strDate,dateTimeFormatter2));
/** String 转 Date */
        System.out.println("String 转 Date: "+Date.from(LocalDateTime.parse(strDateTime,dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant()));

        System.out.println("-------------------------------");

/** Long 转 LocalDateTime */
        System.out.println("Long 转 LocalDateTime:"+LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()));
/** Long 转 LocalDate */
        System.out.println("Long 转 LocalDate:"+LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).toLocalDate());
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.current_date());
        System.out.println(DateUtils.current_datetime());
        System.out.println(DateUtils.current_time());
    }


}
